package com.tarzan.edu.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarzan.common.core.base.Pagination;
import com.tarzan.edu.dao.EduCourseMapper;
import com.tarzan.edu.dao.EduTeacherMapper;
import com.tarzan.edu.entity.EduCourse;
import com.tarzan.edu.entity.EduTeacher;
import com.tarzan.edu.entity.api.EduTeacherApiResp;
import com.tarzan.edu.entity.request.EduTeacherReq;
import com.tarzan.edu.entity.response.EduCourseResp;
import com.tarzan.edu.entity.response.EduTeacherResp;
import com.tarzan.edu.service.IEduTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author liujinshan
 * @title: IEduTeacherServiceImpl
 * @projectName com.tarzan.edu.service.impl
 * @description: TODO 讲师业务层
 * @date 2021/5/21 15:46
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements IEduTeacherService {


    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    @Autowired
    private EduCourseMapper eduCourseMapper;

    /**
     * @param eduTeacher
     * @return void
     * @throws
     * @title insert
     * @description TODO 新增讲师
     * @author liujinshan
     * @data 2021/5/21 15:54
     */
    @Override
    public void insert(EduTeacher eduTeacher) {
        baseMapper.insert(eduTeacher);
    }

    /**
     * @param eduTeacher
     * @return void
     * @throws
     * @title update
     * @description TODO 修改讲师信息
     * @author liujinshan
     * @data 2021/5/21 15:59
     */
    @Override
    public void update(EduTeacher eduTeacher) {
        baseMapper.updateById(eduTeacher);
    }

    /**
     * @param id
     * @return void
     * @throws
     * @title delete
     * @description TODO 删除讲师信息
     * @author liujinshan
     * @data 2021/5/21 16:02
     */
    @Override
    public void delete(String id) {
        baseMapper.deleteById(id);
    }

    /**
     * @param eduTeacherReq
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.tarzan.edu.entity.response.EduTeacherResp>
     * @throws
     * @title findTeacherPageInfo
     * @description TODO 获取分页讲师信息
     * @author liujinshan
     * @data 2021/5/21 16:12
     */
    @Override
    public IPage<EduTeacherResp> findTeacherPageInfo(EduTeacherReq eduTeacherReq) {

        Long currentPage = Optional.of(eduTeacherReq).map(EduTeacherReq::getPagination).map(Pagination::getCurrentPage).orElse(null);
        Long pageSize = Optional.of(eduTeacherReq).map(EduTeacherReq::getPagination).map(Pagination::getPageSize).orElse(null);
        String name = Optional.of(eduTeacherReq).map(EduTeacherReq::getName).orElse(null);
        Integer level = Optional.of(eduTeacherReq).map(EduTeacherReq::getLevel).orElse(null);
        String beginTime = Optional.of(eduTeacherReq).map(EduTeacherReq::getBegin).orElse(null);
        String endTime = Optional.of(eduTeacherReq).map(EduTeacherReq::getEnd).orElse(null);
        IPage page = new Page(currentPage,pageSize);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

/*
        if (!name.isEmpty()){
            queryWrapper.like("name",name);
        }
        if (null != level){

        }

        if (!beginTime.isEmpty()&&!endTime.isEmpty()){
            queryWrapper.between("createTime",beginTime,endTime);
        }
*/
        page =  eduTeacherMapper.selectPage(page,queryWrapper);

        return page;
    }

    /**
     * @param id
     * @return com.tarzan.edu.entity.response.EduTeacherResp
     * @throws
     * @description 根据id查询讲师相细信息
     * @author liujinshan
     * param id
     * @date 2021-5-23 14:47
     */
    @Override
    public EduTeacherResp findTeacherById(String id) {
        EduTeacherResp resp = new EduTeacherResp();

        EduTeacher teacher =  baseMapper.selectById(id);

        BeanUtils.copyProperties(teacher,resp);
        return resp;
    }

    /**
     * @return java.util.List<com.tarzan.edu.entity.response.EduTeacherResp>
     * @title findAllTeacherInfo
     * @description TODO 查询所有讲师信息
     * @author liujinshan
     * @data 2021/8/13 10:25
     */
    @Override
    public List<EduTeacherResp> findAllTeacherInfo() {
        List<EduTeacher> eduTeachers = baseMapper.selectList(null);
        List<EduTeacherResp>  list = JSON.parseArray(JSON.toJSONString(eduTeachers),EduTeacherResp.class);
        return list;
    }

    /**
     * @param id
     * @return com.tarzan.edu.entity.api.EduTeacherApiResp
     * @title findTeacherFrontById
     * @description TODO
     * @author liujinshan
     * @data 2021/10/27 14:10
     */
    @Override
    public EduTeacherApiResp findTeacherFrontById(String id) {
        EduTeacherApiResp eduTeacherApiResp = new EduTeacherApiResp();
        EduTeacher teacher =  baseMapper.selectById(id);
        BeanUtils.copyProperties(teacher,eduTeacherApiResp);
        //2 根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",id);
        List<EduCourse> courseList  = eduCourseMapper.selectList(wrapper);
        List<EduCourseResp> eduCourseRep = JSON.parseArray(JSON.toJSONString(courseList),EduCourseResp.class);
        eduTeacherApiResp.setEduCourseResp(eduCourseRep);
        return eduTeacherApiResp;
    }
}
